from article_app.models import Article
from django_filters import rest_framework as filters

class ArticleFilter(filters.FilterSet):
    #method后跟的函数表示自定义筛选
    # sort = filters.OrderingFilter(fields=('add_time', 'view_no'))
    title = filters.CharFilter(method='title_filter', label="title")
    text = filters.CharFilter(method='text_filter', label="text")
    content_text = filters.CharFilter(method='content_text_filter', label="content_text")
    tags = filters.CharFilter(method='tags_filter', label="tags")


    def title_filter(self, queryset, name, value):
         return queryset.filter(title__icontains=value)

    def text_filter(self, queryset, name, value):
         return queryset.filter(text__icontains=value)
    
    def content_text_filter(self, queryset, name, value):
         return queryset.filter(content_text__icontains=value)
 
    def tags_filter(self, queryset, name, value):
        return queryset.filter(tags__icontains=value)
    
    class Meta:
        model = Article
        fields = ("tags", "text", "content_text")